package com.ty.admin.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.validator.group.SaveGroup;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.vo.CarriersGoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.ty.skysystem.entity.CarriersEntity;
import com.ty.skysystem.service.CarriersService;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;

/**
 * 承运单信息表: 用于存放和管理承运单信息 外键列: user_id => 外键表: User
 *
 * @author tianyuzhu
 * @email 1659770218@QQ.COM
 * @date 2020-12-11 15:56:09
 */
@RestController
@RequestMapping("carriers")
@Slf4j
@Api(tags = "承运单管理模块 用于存放和管理承运单信息")
public class CarriersController {
  @Autowired private CarriersService carriersService;

  /** 查询全部承运信息 */
  @GetMapping("/list")
  @ApiOperation(value = "查询全部承运信息", notes = "查询全部承运信息", response = CarriersEntity.class)
  public R list(@ApiParam("map集合,用于接收分页数据,动态查询的数据") @RequestParam Map<String, Object> params) {
    PageUtils page = carriersService.queryPage(params);

    return R.ok().put(page);
  }

  /** 查询全部已调度的承运信息 */
  @GetMapping("/listScheduled")
  @ApiOperation(value = "查询全部已调度的承运信息", notes = "查询全部已调度的承运信息", response = CarriersEntity.class)
  public R listScheduled(
      @ApiParam("map集合,用于接收分页数据,动态查询的数据") @RequestParam Map<String, Object> params) {
    PageUtils page = carriersService.queryPageScheduled(params);

    return R.ok().put(page);
  }

  /** 查询单个承运信息 */
  @ApiOperation(value = "查询单个承运信息", notes = "查询单个承运信息", response = CarriersEntity.class)
  @GetMapping("/info/{carriersId}")
  public R info(@ApiParam("ID,从URI获取") @PathVariable("carriersId") Long carriersId) {
    QueryWrapper<CarriersEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper.lambda().eq(CarriersEntity::getCarriersId, carriersId);
    queryWrapper.select(CarriersEntity.class, e -> !"is_deleted".equals(e.getColumn()));
    CarriersEntity carriers = carriersService.getOne(queryWrapper);
    return R.ok().put(carriers);
  }

  /** 新增承运信息 */
  @ApiOperation(value = "新增承运信息", notes = "新增承运信息")
  @PostMapping("/save")
  public R save(
      @ApiParam("CarriersGoodsVo实体类,存放需要添加的承运单信息和货物信息") @RequestBody @Validated(SaveGroup.class)
          CarriersGoodsVo carriersGoodsVo) {

    return this.carriersService.saveCarriersGoods(carriersGoodsVo)
        ? R.ok(ResultMsgEnum.INSERT_OK.getMsg())
        : R.ok(ResultMsgEnum.INSERT_ERROR.getMsg());
  }

  /** 修改承运信息 */
  @ApiOperation(value = "修改承运信息", notes = "修改承运信息")
  @PatchMapping("/update")
  public R update(
      @ApiParam("承运单实体类") @RequestBody @Validated(UpdateGroup.class) CarriersEntity carriers) {
    return carriersService.updateById(carriers)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 删除承运信息 */
  @ApiOperation(value = "删除承运信息 ID数组 逻辑删除", notes = "删除承运信息 ID数组 逻辑删除")
  @DeleteMapping("/delete")
  public R delete(@ApiParam("ID数组,逻辑删除") @RequestBody Long[] carriersIds) {
    if (carriersIds.length == 0) return R.error("ID不能为空");
    return carriersService.removeByIds(Arrays.asList(carriersIds))
        ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
        : R.error(ResultMsgEnum.DELETE_ERROR.getMsg());
  }
  /** 已调度承运单进行接收(修改完成情况的状态) */
  @GetMapping("/scheduledCarriers")
  @ApiOperation(
      value = "已调度承运单进行接收(修改完成情况的状态)",
      notes = "已调度承运单进行接收(修改完成情况的状态)",
      response = CarriersEntity.class)
  public R scheduledCarriers(@ApiParam("ID数组,对完成情况的状态进行修改") @RequestBody Long[] carriersIds) {

    return this.carriersService.updateStateByIds(carriersIds)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 历史承运任务查询->查询承运单 */
  @GetMapping("/listHistory")
  @ApiOperation(value = "查询全部历史承运信息", notes = "查询全部历史承运信息", response = CarriersEntity.class)
  public R listHistory(@RequestParam Map<String, Object> params) {

    PageUtils page = carriersService.queryPageHistoryCarriers(params);
    return R.ok().put(page);
  }

  /**
   * 删除历史承运任务
   * @param carriersIds
   * @return
   */
  @DeleteMapping("/deleteHistory")
  @ApiOperation(value = "逻辑删除, id数组, 删除历史承运任务", notes = "逻辑删除, id数组, 删除历史承运任务")
  public R deleteHistory(@ApiParam("Id数组,逻辑删除") @RequestBody Long[] carriersIds) {

    if (carriersIds.length == 0) return R.error("ID不能为空");
    return carriersService.removeByIds(Arrays.asList(carriersIds))
            ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
            : R.error(ResultMsgEnum.DELETE_ERROR.getMsg());
  }
}
